/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.moravian.sound;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

/**
 *
 * @author szilagyi
 */
public class SoundManager
{

    private static final String BASE_PATH = "src/main/resources/sounds/";
    private static final String THEME_FILE_PATH = BASE_PATH + "twrDfndr1_1.wav";
    private AudioStream themeStream;

    public SoundManager()
    {
        loadSounds();
    }

    public void playTheme()
    {
        try
        {
            themeStream = new AudioStream(new FileInputStream(new File(THEME_FILE_PATH)));
        }
        catch (IOException ex)
        {
            System.out.println("FAILED LOADING THEME");
        }
        if (themeStream != null)
        {
            try
            {
                AudioPlayer.player.start(themeStream);
            }
            catch (Exception e)
            {
                e.getMessage().concat("\n Failed to play file " + THEME_FILE_PATH);
            }
        }
    }

    public void stopTheme()
    {
        if (AudioPlayer.player.isAlive())
        {
            AudioPlayer.player.stop(themeStream);
        }
    }

    public static void playFromFile(File file)
    {
        AudioStream stream = null;
        try
        {
            stream = new AudioStream(new FileInputStream(file));
            AudioPlayer.player.start(stream);
        }
        catch (IOException ex)
        {
            System.out.println("Failed to play file " + file.getAbsolutePath());
        }
    }

    public static void playFromAudioStream(AudioStream stream)
    {
        AudioPlayer.player.start(stream);
    }

    private void loadSounds()
    {

        try
        {
            themeStream = new AudioStream(new FileInputStream(new File(THEME_FILE_PATH)));
        }
        catch (IOException ex)
        {
            System.out.println("FAILED LOADING FILES");
        }

    }

}
